<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_aixiaoyi/model')->方法名();
*/
namespace jy_aixiaoyi;
defined('BY_JYA') or exit('error');
class member extends \plugin{
	public function upmember($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member = pdo_get('jy_aixiaoyi_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if(empty($member) && $_SESSION['uid']){
			$coremember=model('core/member')->detail($_SESSION['uid'],array('mobile','groupid','avatar','nickname','sex'));
			$member=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid' => $mid,
				'fromuser'=>$_SESSION['fromuser'],
				'mobile'=>$coremember['mobile'],
				'logo'=>$coremember['avatar'],
				'title'=>$coremember['nickname'],
				'sex'=>$coremember['sex'],
				'createtime'=>time(),
			);
			pdo_insert('jy_aixiaoyi_member',$member);
		}
		return $member;
	}
	public function checkvip($mid=0,$isres=false,$isfeetimes=false){
		$mid=$mid?:$_SESSION['uid'];
		$basic=$this->plugin_setting('basic');
		if($basic['no_vip']){
			return '';
		}
		$member = pdo_get('jy_aixiaoyi_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($member['viptime']<time()){
			$msgnum=pdo_count('jy_aixiaoyi_msglog',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$basic['feetimes']=$isfeetimes?$basic['feetimes2']:$basic['feetimes'];
			if($basic['feetimes'] && $basic['feetimes']<=$msgnum){
				if($isres){
					return 'fail';
				}elseif($basic['no_vip']){
					exi('当前免费次数已用完！','error','jy_aixiaoyi/home/buyvip/');
				}else{
					return 'fail';
				}
			}
		}
		return 'isvip';
	}
	//
	public function bi_up(){
		$basic=$this->plugin_setting('basic');
		if($basic['wenda_time']){
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$msgnum=pdo_count('jy_aixiaoyi_msglog',array('createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));	
			if($basic['wenda_time']<=$msgnum){
				return '当前对话次数已用完！！';
				exi('当前对话次数已用完！！','error');
			}
		}
		$checkvip=$this->checkvip('',true);
		if($checkvip!='isvip'){
			if($basic['bi_open'] && $basic['bi_post']>0){
				$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
				if($basic['bi_post'] >0 && $basic['bi_post']>$member['credit3']){
					return 'fail';
					//return '您的金币不足，不能提问：1、分享好友赚金币；2、看广告赚金币；3、可开通VIP无限对话！';
				}
			}
		}
		return '';
	}
	
	//申请时需要的功能
	public function member_update($mid,$update=array()){
		pdo_update('jy_aixiaoyi_member',$update,array('mid'=>$mid));
		return true;
	}
	
	/*
		申请成为分销商
	*/
	public function apply_agent($mid,$noapply=false){
		$member=$this->pdo_get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
	
		$apply=$this->get('apply',array('mid'=>$mid));
		if($apply && $apply['status']!=2){
			if($apply['status']==1){
				$member_update=array(
					'is_agent'=>1,
					'agenttime'=>TIMESTAMP,
				);
				$this->update('member',$member_update,array('mid'=>$apply['mid']));

			}
			return;
		}
		if($apply['status']!=2){
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'status'=>0,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('apply',$new_apply);
			$apply_id=$this->insertid();
			$this->update('member',array('issq'=>1),array('mid'=>$mid));
		
		}else{
			$this->update('apply',array('status'=>0),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$apply_id=$apply['id'];
		}
		return true;
	}
	
	
	
	/*
		申请成为代理商(推广主)
	*/
	public function check_apply($apply_id,$status,$remark=''){
	    
		if(!$apply_id || !$status){
			return;
		}
		$apply=$this->get('apply',array('id'=>$apply_id));
		if(!$apply){
			return;
		}
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，修改会员状态，修改上线相关数据
			
			$arr1 = array_merge(range('A','Z'),range(0,9),range('a','z')); //生成数组且合并为arr1
            shuffle($arr1); //打乱顺序
            $arr2 = array_rand($arr1,4); 
            $str= "";
            foreach($arr2 as $index){
                $str .=$arr1[$index];
            }
			$member_update=array(
				'is_agent'=>1,
				'agenttime'=>TIMESTAMP,
				'klcode'=>'TG'.$str,
				'status'=>$update['status']
			);
			$this->update('member',$member_update,array('mid'=>$apply['mid']));
			//审核通过，此处增加上线的相关数据
			$agent_id=$this->getcolumn('member',array('mid'=>$apply['mid']),'agent_id');
			for($i=0;$i<3;$i++){
				if(!$agent_id){
					break;
				}
				$agent_update=array('agent_num +='=>1);
				if($i==0){
					$agent_update['agent_num_next +=']=1;
				}
				$this->update('member',$agent_update,array('mid'=>$agent_id));
				$agent_id=$this->getcolumn('member',array('mid'=>$agent_id),'agent_id');
			}
		}else{
			$update['remark']=$remark;
		}
		$this->update('apply',$update,array('id'=>$apply_id));
		return true;
	}
	
	/*
		禁用分销商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id));
	}
	
	/*
		恢复分销商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id));
	}
	
	public function getteamids($teammid=0,$level=0){
		$teammid=$teammid?:$_SESSION['uid'];
		$newmembers=array($teammid=>$teammid);
		$newteammids=$teammid;
		$level=$level?:7;
		for($i=1;$i<$level;$i++){
			$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'agent_id'=>$newteammids),array('mid'),'mid');
			if($members){
				foreach($members as $key=>$item){
					$newmembers[$key]=$key;
				}
				$newteammids=array_keys($members);
			}else{
				break;
			}
		}
		if($newmembers){
			$teamids=array_keys($newmembers);
		}else{
			$teamids=array();
		}
		return $teamids;
	}
}
?>